package org.marketing.common.util;
import org.marketing.common.dto.maintain.SosHelpDealerInfoDto;

public class PointUtile {
	public static int raidus = 200000;// 默认5公里范围
	private static final double EARTH_RADIUS = 6378137;

	/**
	 * 生成以中心点为中心的四方形经纬度
	 * 
	 * @param lat
	 *            纬度
	 * @param lon
	 *            精度
	 * @param raidus
	 *            半径（以米为单位）
	 * @return
	 */
	public static double[] getAround(double lat, double lng, int raidus) {
		Double latitude = lat;
		Double longitude = lng;
		Double degree = (24901 * 1609) / 360.0;
		double raidusMile = raidus;
		Double dpmLat = 1 / degree;
		Double radiusLat = dpmLat * raidusMile;
		Double minLat = latitude - radiusLat;
		Double maxLat = latitude + radiusLat;
		Double mpdLng = degree * Math.cos(latitude * (Math.PI / 180));
		Double dpmLng = 1 / mpdLng;
		Double radiusLng = dpmLng * raidusMile;
		Double minLng = longitude - radiusLng;
		Double maxLng = longitude + radiusLng;
		return new double[] { minLat, minLng, maxLat, maxLng };
	}

	/**
	 * 返回两个经纬度距离多少米
	 */
	public static double GetDistance(double lng1, double lat1, double lng2, double lat2) {
		double radLat1 = rad(lat1);
		double radLat2 = rad(lat2);
		double a = radLat1 - radLat2;
		double b = rad(lng1) - rad(lng2);
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return s;
	}
	public static void main(String[] args) {
		System.out.println(GetDistance(116.404022,39.913922,116.363526,39.948003));
	}
	public static double GetDistance(String lat, String lng, SosHelpDealerInfoDto sosDealer) {
		double radLat1 = rad(Double.parseDouble(lat));
		double radLat2 = rad(sosDealer.getLat());
		double a = radLat1 - radLat2;
		double b = rad(Double.parseDouble(lng)) - rad(sosDealer.getLng());
		double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2)
				* Math.pow(Math.sin(b / 2), 2)));
		s = s * EARTH_RADIUS;
		s = Math.round(s * 10000) / 10000;
		return s;
	}
	
	

	private static double rad(double d) {
		return d * Math.PI / 180.0;
	}

}
